package com.house365.core.adapter;

import java.util.HashMap;
import java.util.Map;

import android.content.Context;
import android.widget.ImageView;

import com.house365.core.application.BaseApplication;
import com.house365.core.image.AsyncImageLoader;
import com.house365.core.image.CacheImageUtil;
import com.house365.core.image.CacheImageUtil.ImageOperate;

public abstract class BaseCacheListAdapter<T> extends BaseListAdapter<T> {

	protected BaseApplication application;
	protected AsyncImageLoader mAil;
	protected Context context;
	private boolean hasFirstScrolled = false;
	public Map<Integer,Object> viewReferences = new HashMap<Integer, Object>();
	public BaseCacheListAdapter(Context context) {
		super(context);
		this.context = context;
		application = (BaseApplication)context.getApplicationContext();
		mAil = new AsyncImageLoader(context);
	}

	public void clear() {
		super.clear();
	}
	
	public void setCacheImage(final ImageView imageView,String imageUrl,final int resId,int scaleType) {
		CacheImageUtil.setCacheImage(imageView, imageUrl, resId, scaleType, mAil);
	}
	public void setCacheImageWithImageOper(final ImageView imageView,String imageUrl,final int resId,int scaleType,ImageOperate imageOperate){
		CacheImageUtil.setCacheImageWithImageOper(imageView, imageUrl, imageOperate , context.getResources(), resId, scaleType, mAil);
	}
	public void setCacheImageWithoutDef(final ImageView imageView,String imageUrl,int scaleType){
		CacheImageUtil.setCacheImageWithoutDef(imageView, imageUrl, scaleType, mAil);
	}
	public void setCacheImageFromLocalFile(final ImageView imageView,String localFile,final int resId,int scaleType) {
		CacheImageUtil.setCacheImageFromLocalFile(imageView, localFile, resId, scaleType, mAil);
	}
	public boolean hasFirstScrolled(){
		return hasFirstScrolled;
	}
	public void setHasFirstScrolled(boolean f){
		this.hasFirstScrolled = f;
	}
	//当停止滚动
	public void onScrollIdle(int startIdx,int endIdx){
//		for(int i = startIdx;i<=endIdx;i++){
//			if(viewReferences.get(i) != null){
//				onScrollIdleEvent(i,viewReferences.get(i));	
//			}
//			
//		}
	}
	/**
	 * 为了提高效率，如果需要lazy load的列表数据，必须实现该方法，如果不需要使用该事件，子类里面，方法体为空
	 * @param position,ListView 的position
	 * @param viewReference，用户存放的view的应用，正常为ViewHolder
	 */
//	public abstract void onScrollIdleEvent1(int position,Object viewReference);
	/**
	public void putViewReference(int position,Object o){
		if(viewReferences.get(position) != null && viewReferences.get(position)  == o){
			//存放的就是正确的，不需要变动
			return;
		}
		Integer key =-1;
		if(viewReferences.containsValue(o)){
			//已经存在该对象,需要移除他
			for(Entry<Integer,Object> entry: viewReferences.entrySet()){
				if(entry.getValue() == o){
					key = entry.getKey();
				}
			}
		}
		if(key >0){
			viewReferences.remove(key);
		}
		viewReferences.put(position, o);
	}
	@Override
	public void notifyDataSetChanged() {
		setHasFirstScrolled(false);
		super.notifyDataSetChanged();
	}
	*/
}
